package com.winning.devops.batch.writer.ch02.config;

import com.winning.devops.batch.writer.ch02.domain.Customer;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 数据库Writer 配置
 *
 * 基于Jdbc来写入数据
 * 从customer.txt中读取文件，写入数据库中
 *
 * @author chensj
 * @date 2020-03-07 15:30
 */
@Configuration
public class JdbcWriterDemoJobConfig {


    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    @Qualifier("jdbcFlatFileItemReader")
    private ItemReader<? extends Customer> jdbcFlatFileItemReader;
    @Autowired
    @Qualifier("jdbcDemoItemWriter")
    private ItemWriter<? super Customer> jdbcDemoItemWriter;


    @Bean
    public Job jdbcWriterDemoJob() {
        return jobBuilderFactory.get("jdbcWriterDemoJob")
                .start(jdbcWriterDemoStep())
                .build();
    }

    @Bean
    public Step jdbcWriterDemoStep() {
        return stepBuilderFactory
                .get("jdbcWriterDemoStep")
                .<Customer,Customer>chunk(10)
                .reader(jdbcFlatFileItemReader)
                .writer(jdbcDemoItemWriter)
                .build();
    }


}
